/*
	Copyright 2008 Jay Elliott

	This file is part of libemath.

	libemath is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	libemath is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with libemath.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "cSphere.h"



bool emath::cSphere::InFrustum(const emath::cPlane frust[6]) const//TODO: untested
{
	for (int i = 0; i < 6; i++)
	{
		float dist = frust[i].DistanceTo(pos);
		if (dist <= -rad/* - EPSILON*/)
			return false;
	}
	return true;
}

bool emath::cSphere::intersects(emath::cAlignedBox box)
{
     float dist = 0.0f;
     float buffer;
     cPoint3d BoxPos = box.GetPos();
     cPoint3d BoxExt = box.GetExt();
     
     for (int i = 0; i < 3; i++)
     {
         if (pos[i] > BoxPos[i] + BoxExt[i])
         {
           buffer = pos[i] - (BoxPos[i] + BoxExt[i]);
           dist += buffer * buffer;
         }
         else if (pos[i] < BoxPos[i] - BoxExt[i])
         {
              buffer = pos[i] - (BoxPos[i] - BoxExt[i]);
              dist += buffer * buffer;
         }
     }
     
     return dist <= rad * rad;
}
